package com.zmyuan.quartz.ram.demo;

import org.quartz.CalendarIntervalScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.DailyCalendar;

import java.util.Properties;

import static org.quartz.JobBuilder.newJob;

/**
 * Created by zhudebin on 2017/2/25.
 */
public class CalendarIntervalScheduleDemo {

    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.load(CalendarIntervalScheduleDemo.class.getResourceAsStream("/quartz.properties"));

        StdSchedulerFactory schedFact = new StdSchedulerFactory(properties);


        Scheduler sched = schedFact.getScheduler();
        sched.start();

        // define the job and tie it to our HelloJob class
        JobDetail job = newJob(HelloJob.class)
                .withIdentity("myJob", "group1")
                .usingJobData("squidFlowId", 111)
                .build();

        DailyCalendar calendar = new DailyCalendar(11, 0, 0, 0, 20, 0, 0, 0);
        calendar.setInvertTimeRange(true);
        String cstr = calendar.toString();
        System.out.println(cstr);
        sched.addCalendar("abcd", calendar, true, true);


        // 触发时间点
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder
                .simpleSchedule()
                .withRepeatCount(4).withIntervalInSeconds(3);
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(simpleScheduleBuilder)
                .modifiedByCalendar("abcd").build();

        // Tell quartz to schedule the job using our trigger

        sched.scheduleJob(job, trigger);

        System.out.println("schedule job");
    }
}
